package com.ruigi.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruigi.Dto.DishDto;
import com.ruigi.pojo.Category;
import com.ruigi.pojo.Dish;
import com.ruigi.pojo.DishFlavor;
import com.ruigi.pojo.R;
import com.ruigi.service.CategoryService;
import com.ruigi.service.DishflaverService;
import com.ruigi.service.Dishservice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class Dishcontrol {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private Dishservice dishservice;
    @Autowired
    private DishflaverService dishflaverService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save1(@RequestBody DishDto dishDto) {

        dishservice.savewithfalor(dishDto);
        String keys = "dish_" + dishDto.getCategoryId() + "_1";
        //清理所有菜品缓存

        redisTemplate.delete(keys);


        return R.success("新增菜品成功");


    }

    @GetMapping("/page")

    public R<Page> page(int page, int pageSize, String name) {


        return dishservice.page1(page, pageSize, name);

    }

    //query information based on the id
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id) {


        return dishservice.getid(id);
    }

    @PutMapping

    public R<String> update(@RequestBody DishDto dishDto) {


        dishservice.updateflavor(dishDto);
        String keys = "dish_" + dishDto.getCategoryId() + "_1";
        //清理所有菜品缓存

        redisTemplate.delete(keys);

        return R.success("更新菜品成功");


    }

    @PostMapping("/status/{stat}")
    public R<String> sale(@PathVariable int stat, Long ids[]) {

        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setStatus(stat);
            dish.setId(id);
            dishservice.updateById(dish);

        }


        Set keys = redisTemplate.keys("dish_*");
        //清理所有菜品缓存

        redisTemplate.delete(keys);
        return R.success("停售成功");

    }

    @DeleteMapping
    public R<String> delete(Long ids[]) {
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dishservice.removeById(dish);
        }


        return R.success("删除成功");
    }

//    @GetMapping("/list")
//    public R<List<Dish>> list(Dish dish) {
//
//        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Dish::getCategoryId, dish.getCategoryId());
//        List<Dish> list = dishservice.list(queryWrapper);
//
//        return R.success(list);
//
//    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish) {
        List<DishDto> collect = null;
        String key = "dish_" + dish.getCategoryId() + "_" + dish.getStatus();
        //先从redis中获得数据
        collect = (List<DishDto>) redisTemplate.opsForValue().get(key);
        if (collect != null) {
            //如果存在，不查询数据库
            return R.success(collect);


        }

        //如果不存在，查询数据库并存在缓存

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, dish.getCategoryId());
        List<Dish> list = dishservice.list(queryWrapper);

        collect = list.stream().map(re -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(re, dishDto);

            Long id = re.getCategoryId();
            Category byId = categoryService.getById(id);
            if (byId != null) {
                String name1 = byId.getName();
                dishDto.setCategoryName(name1);

            }
            Long dishid = re.getId();
            LambdaQueryWrapper<DishFlavor> flavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            flavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dishid);
            List<DishFlavor> list1 = dishflaverService.list(flavorLambdaQueryWrapper);
            dishDto.setFlavors(list1);

            return dishDto;
        }).collect(Collectors.toList());

        redisTemplate.opsForValue().set(key, collect, 60, TimeUnit.MINUTES);
        return R.success(collect);

    }


}
